﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpMUD.Framework.CorlibExtensions
{
    public class UpcastingEnumerator<T,U>
        : IEnumerator<T>
        where U : T
    {
        private IEnumerator<U> _UnderlyingEnumerator;

        public UpcastingEnumerator(IEnumerator<U> underlyingEnumerator)
        {
            if (underlyingEnumerator == null)
                throw new ArgumentNullException("underlyingEnumerator");
            _UnderlyingEnumerator = underlyingEnumerator;
        }

        #region IEnumerator<T> Members

        public T Current
        {
            get { return (T)_UnderlyingEnumerator.Current; }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            _UnderlyingEnumerator.Dispose();
            _UnderlyingEnumerator = null;
        }

        #endregion

        #region IEnumerator Members

        object System.Collections.IEnumerator.Current
        {
            get { return _UnderlyingEnumerator.Current; }
        }

        public bool MoveNext()
        {
            return _UnderlyingEnumerator.MoveNext();
        }

        public void Reset()
        {
            _UnderlyingEnumerator.Reset();
        }

        #endregion
    }
}
